package de.fhaugsburg.games.boardgames.scrabble;

import java.util.ArrayList;
import java.util.List;

import de.fhaugsburg.games.boardgames.Board;
import de.fhaugsburg.games.boardgames.Player;
import de.fhaugsburg.games.boardgames.Player.PlayerType;
import de.fhaugsburg.games.boardgames.exceptions.GameException;
import de.fhaugsburg.games.boardgames.exceptions.IllegalMoveException;
import de.fhaugsburg.games.boardgames.scrabble.scrabbleboard.LetterPiece;
import de.fhaugsburg.games.boardgames.scrabble.scrabbleboard.ScrabbleBoard;


public class ScrabblePlayer extends Player {

	public static final int MAX_PIECES = 7;
	
	private List<LetterPiece> myPieces = new ArrayList<LetterPiece>(MAX_PIECES);
	private int credits = 0;
	private boolean cpuPlayer;
	
	
	//TODO
	private int countPieces = 0;
	private IScrabbleBot scrabbleBot;

	
		
	/**
	 * constructor
	 * @param name
	 * @param board
	 */
	public ScrabblePlayer(String name, ScrabbleBoard board, PlayerType playerType) {
		super(name, (Board) board, playerType);
	}	

		
	
	/**
	 * constructor for cpuplayer
	 * @param name
	 * @param board
	 * @param scrabbleBot
	 */
	public ScrabblePlayer(String name, ScrabbleBoard board, PlayerType playerType, IScrabbleBot scrabbleBot) {
		this(name, board, playerType);
		this.setCpuPlayer(scrabbleBot);
	}

	
	

	/**
	 * recieves a new letterpiece
	 * @param piece
	 * @throws GameException
	 */
	public void recieve(LetterPiece piece) throws IllegalMoveException{
		if (myPieces.size() < MAX_PIECES){
			myPieces.add(piece);
		}else{
			//TODO
			throw new IllegalMoveException("cannot hold more than " + MAX_PIECES + "");
		}
	}
	
	
	
	/**
	 * recieve a whole list of letterpieces
	 * @param newPieces
	 */
	public void recieveAll (List<LetterPiece> newPieces){
		this.myPieces.addAll(newPieces);
	}
	
	
	/**
	 * give a piece away
	 * @param piece
	 * @throws GameException
	 */
	public void giveAway(LetterPiece piece) throws IllegalMoveException{
		if (myPieces.contains(piece)){
			myPieces.remove(piece);
		}else{
			throw new IllegalMoveException("piece not available");
		}
	}
	
	
	/**
	 * exchange piece
	 * @param oldPiece
	 * @param newPiece
	 * @throws GameException
	 */
	public void exchangePiece(LetterPiece oldPiece, LetterPiece newPiece) throws IllegalMoveException{
		giveAway(oldPiece);
		recieve(newPiece);
	}
	
	
	/**
	 * exchange all pieces 
	 * @param newPieces
	 * @throws IllegalMoveException
	 */
	public void exchangeAllPieces(List<LetterPiece> newPieces) throws IllegalMoveException {
		//give away old Pieces
		List<LetterPiece> allMyPieces = new ArrayList<LetterPiece>(this.myPieces);
		for (LetterPiece piece : allMyPieces) {
			giveAway(piece);
		}
		//get new Pieces
		this.recieveAll(newPieces);
	}
	
	
	/**
	 * returns all pieces
	 * @return
	 */
	public List<LetterPiece> getMyPieces(){
		return this.myPieces;
	}
	
	
	
	/**
	 * player has this piece?
	 * @param piece
	 * @return
	 */
	public boolean hasPiece(LetterPiece piece){
		return getMyPieces().contains(piece);
	}
	
	
	/**
	 * returns String of all pieces
	 * @return
	 */
	public String getMyPiecesString(){
		return this.myPieces.toString();
	}


	
	/**
	 * credits for me!!
	 * @param points
	 */
	public void addCredits(int credits){
		this.credits += credits;
	}
	
		


	/**
	 * @return the credits
	 */
	public int getCredits() {
		return credits;
	}



	/**
	 * @param cpuPlayer the cpuPlayer to set
	 */
	private void setCpuPlayer(IScrabbleBot scrabbleBot) {
		this.cpuPlayer = true;
		this.scrabbleBot = scrabbleBot;
	}


	/**
	 * returns the bot instance
	 * @return
	 */
	public IScrabbleBot getCpuPlayer(){
		return this.scrabbleBot;
	}
	
	

	/**
	 * @return the cpuPlayer
	 */
	public boolean isCpuPlayer() {
		return cpuPlayer;
	}
	
	
	
}
